package graphic.mycomponents;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import javax.swing.JScrollPane;
import javax.swing.JViewport;

import dbms.DBSelectingFunctions;
import filemanager.FileManager;

public class ProgramTab extends JScrollPane {

	public static String NONE = "None";
	
	FileManager fileManager;
	
	ProgramTable programTable;
	
	public ProgramTab(FileManager fileManager)
	{
		this.fileManager = fileManager;

		this.createTable();
	}
	
	public void createTable()
	{
		this.programTable = new ProgramTable();
		
		if(this.fileManager != null)
			this.populateTable();
		
		super.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);
		super.setViewportView(this.programTable);
	}
	
	protected void populateTable()
	{
		try {
			ResultSet ris = DBSelectingFunctions.getFiles(this.fileManager.getProgramName());
			
			HashMap<String, String> lockOwners = this.fileManager.getLockOwners();
			
			String lockedBy;
			while(ris.next())
			{
				lockedBy = lockOwners.get(ris.getString("name"));
				if(lockedBy == null)
					this.programTable.getModel().insertRow(this.programTable.getRowCount(), new Object[]{ris.getInt("id"), ris.getString("name"), ris.getString("last_edit_by"), ris.getString("last_edit_when"), ris.getString("release_number"), ProgramTab.NONE, new Boolean(false)});
				else
					this.programTable.getModel().insertRow(this.programTable.getRowCount(), new Object[]{ris.getInt("id"), ris.getString("name"), ris.getString("last_edit_by"), ris.getString("last_edit_when"), ris.getString("release_number"), lockedBy, new Boolean(true)});
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	
	public String getSelectedFileName()
	{
		int selectedRow = this.programTable.getSelectedRow();
		if(selectedRow == -1) return "";
		
		return (String)this.programTable.getValueAt(selectedRow, 1);	
	}
}
